﻿Public Class ProjectImageAddForm

    Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。

    End Sub

    Sub New(ByVal pID As String)
        MyClass.New()

        _pid = pID
    End Sub

    Sub New(ByVal pii As ProjectImageItem)

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        _pii = pii

        Me.Text = "修改图层"

        Init()
    End Sub

    
    Private _pid As String = ""
   
    ''' <summary>
    ''' 初始化图层信息
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub Init()
        Try
            If System.IO.File.Exists(_pii.imagePath) Then
                Me.PictureBox1.Image = Image.FromFile(_pii.imagePath)
            End If
        Catch ex As Exception
            Debug.WriteLine(ex.Message)
        End Try

        Me.txtlayName.Text = _pii.imageName
        Me.nudOrder.Value = _pii.ImageOrder
        Me.txtPath.Text = _pii.imagePath

    End Sub

    Private Sub ProjectImageAddForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub txtlayName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtlayName.TextChanged, txtPath.TextChanged
        If txtlayName.Text.Trim <> "" AndAlso Me.txtPath.Text.Trim <> "" Then
            Me.btnOk.Enabled = True
        Else
            Me.btnOk.Enabled = True
        End If
    End Sub

    Private Sub btnBrowser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowser.Click
        Dim dlg As New OpenFileDialog()
        dlg.Filter = "JPEG文件(*.jpg)|*.jpg|位图文件(*.bmp)|*.bmp|GIF文件(*.gif)|*.gif|所有文件(*.*)|*.*"

        If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.txtPath.Text = dlg.FileName
            Me.PictureBox1.Image = Image.FromFile(dlg.FileName)
        End If
    End Sub

    Private Function CheckName() As Boolean
        Return True
    End Function


    Private _pii As ProjectImageItem
    Public Property pii() As ProjectImageItem
        Get
            Return _pii
        End Get
        Set(ByVal value As ProjectImageItem)
            _pii = value
        End Set
    End Property


    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        If CheckValidate() = False Then
            Exit Sub
        End If

        If Me._pii Is Nothing Then
            '新增图片
            AddImage()
        Else
            '修改图片
            ModifyImage()
        End If

    End Sub


    ''' <summary>
    ''' 增加图层
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub AddImage()

        Dim pii As New ProjectImageItem

        pii.projectID = _pid
        pii.imageName = txtlayName.Text.Trim
        pii.ImageOrder = nudOrder.Value
        pii.imagePath = txtPath.Text.Trim
        pii.ImageID = Guid.NewGuid.ToString()

        If ProjectImageService.Create(pii) = 1 Then
            _pii = pii

            'Log.log(tbccLogOperateType.ModifyProject, String.Format("增加图层{0}", pii.imageName))

            Me.DialogResult = Windows.Forms.DialogResult.OK
            Me.Close()
        End If
    End Sub


    ''' <summary>
    ''' 修改图层
    ''' </summary>
    ''' <remarks></remarks>

    Private Sub ModifyImage()
        _pii.imageName = txtlayName.Text.Trim
        _pii.ImageOrder = nudOrder.Value
        _pii.imagePath = txtPath.Text.Trim

        If ProjectImageService.Update(pii) = 1 Then

            'Log.log(tbccLogOperateType.ModifyProject, String.Format("修改图层{0}", pii.imageName))

            Me.DialogResult = Windows.Forms.DialogResult.OK
            Me.Close()
        End If
    End Sub


    ''' <summary>
    ''' 检查输入数据的合法性
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function CheckValidate() As Boolean

        If Me.txtlayName.Text.Trim = "" Then
            msg.Show("请输入图层名称。")
            Me.txtlayName.Focus()
            Return False
        End If

        If Me.txtPath.Text.Trim = "" Then
            msg.Show("请选择工程图")
            Me.btnBrowser.Focus()
            Return False
        End If

        If CheckName() = False Then
            msg.ShowWarning(String.Format("图层名【{0}】已经存在，请输入其它名称。", Me.txtlayName.Text.Trim))
            Me.txtlayName.SelectionStart = 0
            Me.txtlayName.SelectionLength = 9999
            Me.txtlayName.Focus()
            Return False
        End If

        Return True

    End Function


    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub
End Class